package com.lanyou.esb.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Tuple;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import com.lanyou.cook.entity.domain.Field;
import com.lanyou.cook.entity.domain.FieldSystem;
import com.lanyou.cook.util.gson.GsonUtil;
import com.lanyou.esb.common.CommonUtils;
import com.lanyou.esb.datadictionary.AuditStatusType;
import com.lanyou.esb.datadictionary.EnableType;
import com.lanyou.esb.datadictionary.IfOperationType;
import com.lanyou.esb.entity.Http;
import com.lanyou.esb.entity.IfParameter;
import com.lanyou.esb.entity.Token;
import com.lanyou.esb.web.quantityControlcontrol.vo.QuantityControlVo;

/**
 * HTTP接口服务类
 * 
 * @author Davey.wu
 */
@Service
public class HttpService {

	@PersistenceContext
	private EntityManager entityManager;

	/**
	 * 获取接口信息
	 * 
	 * @param interfaceId
	 *            ：接口标识
	 * @param auditStatus
	 *            ：审核状态
	 * @param isEnable
	 *            ：是否可用
	 * @return
	 */
	public Http getHttp(String interfaceId, String auditStatus, String isEnable) {
		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		CriteriaQuery<Tuple> cq = cb.createTupleQuery();

		// 构建FROM和JOIN部分
		Root<Http> rootInterface = cq.from(Http.class);

		// 构建SELECT部分
		cq.multiselect(rootInterface.get("id").alias("interfaceId"),
				rootInterface.get("code").alias("code"),
				rootInterface.get("name").alias("interfaceName"), rootInterface
						.get("priUrl").alias("priUrl"),
				rootInterface.get("pubUrl").alias("pubUrl"),
				rootInterface.get("request").alias("request"), rootInterface
						.get("dataFormate").alias("dataFormate"), rootInterface
						.get("type").alias("type"),
				rootInterface.get("authType").alias("authType"), rootInterface
						.get("requestMess").alias("requestMess"), rootInterface
						.get("contentType").alias("contentType"), rootInterface
						.get("systemCode").alias("systemCode"), rootInterface
						.get("version").alias("version"),
				rootInterface.get("operationType").alias("operationType"),
				rootInterface.get("soapActionUri").alias("soapActionUri"));

		// 构建WHERE部分
		List<Predicate> predicatesList = new ArrayList<>();
		if (StringUtils.isNotBlank(interfaceId)) {
			predicatesList.add(cb.equal(rootInterface.get("id"), interfaceId));
		}
		if (StringUtils.isNotBlank(auditStatus)) {
			predicatesList.add(cb.equal(rootInterface.get("auditStatus"),
					auditStatus));
		}
		if (StringUtils.isNotBlank(isEnable)) {
			predicatesList
					.add(cb.equal(rootInterface.get("isEnable"), isEnable));
		}
		if (predicatesList.size() > 0) {
			cq.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
		}
		// 执行查询
		TypedQuery<Tuple> q = entityManager.createQuery(cq);
		List<Tuple> queryResult = q.getResultList();

		if (queryResult == null || queryResult.size() < 1) {
			return null;
		}

		Tuple tuple = queryResult.get(0);
		Http http = new Http();
		http.setId((String) tuple.get("interfaceId"));
		http.setCode((String) tuple.get("code"));
		http.setName((String) tuple.get("interfaceName"));
		http.setPriUrl((String) tuple.get("priUrl"));
		http.setPubUrl((String) tuple.get("pubUrl"));
		http.setType((String) tuple.get("type"));
		http.setDataFormate((String) tuple.get("dataFormate"));
		http.setRequest((String) tuple.get("request"));
		http.setRequestMess((String) tuple.get("requestMess"));
		http.setAuthType((String) tuple.get("authType"));
		http.setContentType((String) tuple.get("contentType"));
		http.setSystemCode((String) tuple.get("systemCode"));
		http.setVersion((String) tuple.get("version"));
		http.setOperationType((String) tuple.get("operationType"));
		http.setSoapActionUri((String) tuple.get("soapActionUri"));
		return http;
	}

	/**
	 * 获取接口信息
	 * 
	 * @param ifIdList
	 *            ：接口标识列表
	 * @param auditStatus
	 *            ：审核状态
	 * @param isEnable
	 *            ：是否可用
	 * @return
	 */
	public List<Http> getHttpInterfaceInfos(List<String> ifIdList,
			String auditStatus, String isEnable) {
		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		CriteriaQuery<Tuple> cq = cb.createTupleQuery();

		// 构建FROM和JOIN部分
		Root<Http> rootHttp = cq.from(Http.class);
		Join<Http, FieldSystem> joinSystem = rootHttp.join("fieldSystem",
				JoinType.LEFT);
		Join<FieldSystem, Field> joinField = joinSystem.join("field",
				JoinType.LEFT);
		// 构建SELECT部分
		cq.multiselect(rootHttp.get("id").alias("interfaceId"),
				rootHttp.get("code").alias("code"),
				rootHttp.get("name").alias("interfaceName"),
				rootHttp.get("priUrl").alias("priUrl"), rootHttp.get("pubUrl")
						.alias("pubUrl"),
				rootHttp.get("request").alias("request"),
				rootHttp.get("dataFormate").alias("dataFormate"),
				rootHttp.get("type").alias("type"), rootHttp.get("version")
						.alias("version"),
				rootHttp.get("contentType").alias("contentType"),
				rootHttp.get("requestMess").alias("requestMess"),
				rootHttp.get("operationType").alias("operationType"), rootHttp
						.get("soapActionUri").alias("soapActionUri"), rootHttp
						.get("createTime").alias("createTime"),
				rootHttp.get("desc").alias("desc"), rootHttp.get("remark")
						.alias("remark"), joinSystem.get("id")
						.alias("systemId"),
				joinSystem.get("code").alias("systemCode"),
				joinSystem.get("name").alias("systemName"), joinField.get("id")
						.alias("fieldId"),
				joinField.get("name").alias("fieldName"));

		// 构建WHERE部分
		List<Predicate> predicatesList = new ArrayList<>();
		if (ifIdList != null && ifIdList.size() > 0) {
			predicatesList.add(rootHttp.get("id").in(ifIdList));
		}
		if (StringUtils.isNotBlank(auditStatus)) {
			predicatesList.add(cb.equal(rootHttp.get("auditStatus"),
					auditStatus));
		}
		if (StringUtils.isNotBlank(isEnable)) {
			predicatesList.add(cb.equal(rootHttp.get("isEnable"), isEnable));
		}
		if (predicatesList.size() > 0) {
			cq.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
		}

		// 执行查询
		TypedQuery<Tuple> q = entityManager.createQuery(cq);
		List<Tuple> queryResult = q.getResultList();

		if (queryResult == null || queryResult.size() < 1) {
			return null;
		}
		List<Http> https = new ArrayList<Http>();

		for (Tuple tuple : queryResult) {
			Http http = new Http();
			FieldSystem system = new FieldSystem();
			system.setId((String) tuple.get("systemId"));
			system.setCode((String) tuple.get("systemCode"));
			system.setName((String) tuple.get("systemName"));
			http.setFieldSystem(system);
			Field field = new Field();
			field.setId((String) tuple.get("fieldId"));
			field.setName((String) tuple.get("fieldName"));
			system.setField(field);
			http.setId((String) tuple.get("interfaceId"));
			http.setCode((String) tuple.get("code"));
			http.setName((String) tuple.get("interfaceName"));
			http.setPriUrl((String) tuple.get("priUrl"));
			http.setPubUrl((String) tuple.get("pubUrl"));
			http.setType((String) tuple.get("type"));
			http.setDataFormate((String) tuple.get("dataFormate"));
			http.setRequest((String) tuple.get("request"));
			http.setRequestMess((String) tuple.get("requestMess"));
			http.setContentType((String) tuple.get("contentType"));
			if (system != null) {
				http.setSystemCode(system.getCode());
			}
			http.setVersion((String) tuple.get("version"));
			http.setOperationType((String) tuple.get("operationType"));
			http.setSoapActionUri((String) tuple.get("soapActionUri"));
			http.setCreateTime(tuple.get("createTime") == null ? null
					: ((Date) tuple.get("createTime")));
			http.setDesc((String) tuple.get("desc"));
			http.setRemark((String) tuple.get("remark"));
			https.add(http);
		}
		return https;
	}

	/**
	 * 获取接口信息
	 * 
	 * @param ifIdList
	 *            ：接口标识列表
	 * @param auditStatus
	 *            ：审核状态
	 * @param isEnable
	 *            ：是否可用
	 * @param operationType
	 *            ：接口操作类型
	 * @param currentPage
	 *            ：当前页
	 * @param pageSize
	 *            ：页大小
	 * @return
	 */
	public Map<String, Http> getHttpInterfaceInfos(List<String> ifIdList,
			String auditStatus, String isEnable, String operationType,
			int currentPage, int pageSize) {
		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		CriteriaQuery<Tuple> cq = cb.createTupleQuery();

		// 构建FROM和JOIN部分
		Root<Http> rootIf = cq.from(Http.class);
		Join<Http, Token> joinToken = rootIf.join("interfaceToken",
				JoinType.LEFT);
		Join<Http, FieldSystem> joinSystem = rootIf.join("fieldSystem",
				JoinType.LEFT);
		Join<FieldSystem, Field> joinField = joinSystem.join("field",
				JoinType.LEFT);
		// 构建SELECT部分
		cq.multiselect(rootIf.get("id").alias("interfaceId"), rootIf
				.get("code").alias("code"),
				rootIf.get("name").alias("interfaceName"), rootIf.get("priUrl")
						.alias("priUrl"), rootIf.get("pubUrl").alias("pubUrl"),
				rootIf.get("request").alias("request"),
				rootIf.get("dataFormate").alias("dataFormate"),
				rootIf.get("type").alias("type"),
				rootIf.get("status").alias("status"), rootIf.get("requestMess")
						.alias("requestMess"),
				rootIf.get("contentType").alias("contentType"),
				rootIf.get("version").alias("version"),
				rootIf.get("operationType").alias("operationType"),
				rootIf.get("soapActionUri").alias("soapActionUri"), joinToken
						.get("appKey").alias("applyKey"), joinSystem.get("id")
						.alias("systemId"),
				joinSystem.get("code").alias("systemCode"), joinField.get("id")
						.alias("fieldId"),
				joinField.get("name").alias("fieldName"));

		// 构建WHERE部分
		List<Predicate> predicatesList = new ArrayList<>();
		if (ifIdList != null && ifIdList.size() > 0) {
			predicatesList.add(rootIf.get("id").in(ifIdList));
		}
		if (StringUtils.isNotBlank(auditStatus)) {
			predicatesList
					.add(cb.equal(rootIf.get("auditStatus"), auditStatus));
		}
		if (StringUtils.isNotBlank(isEnable)) {
			predicatesList.add(cb.equal(rootIf.get("isEnable"), isEnable));
		}
		if (StringUtils.isNotBlank(operationType)) {
			predicatesList.add(cb.equal(rootIf.get("operationType"),
					operationType));
		}
		if (predicatesList.size() > 0) {
			cq.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
		}

		cq.orderBy(cb.desc(rootIf.get("id")));
		// 执行查询
		TypedQuery<Tuple> q = entityManager.createQuery(cq);
		q.setFirstResult(currentPage * pageSize);
		q.setMaxResults(pageSize);

		List<Tuple> queryResult = q.getResultList();

		if (queryResult == null || queryResult.size() < 1) {
			return null;
		}
		Map<String, Http> https = new HashMap<String, Http>();

		for (Tuple tuple : queryResult) {
			Http http = new Http();
			Token token = new Token();
			FieldSystem system = new FieldSystem();
			system.setId((String) tuple.get("systemId"));
			system.setCode((String) tuple.get("systemCode"));
			http.setFieldSystem(system);
			Field field = new Field();
			field.setId((String) tuple.get("fieldId"));
			field.setName((String) tuple.get("fieldName"));
			system.setField(field);
			token.setAppKey((String) tuple.get("applyKey"));
			Set<Token> set = new HashSet<Token>();
			set.add(token);
			http.setInterfaceToken(set);
			http.setId((String) tuple.get("interfaceId"));
			http.setCode((String) tuple.get("code"));
			http.setName((String) tuple.get("interfaceName"));
			http.setPriUrl((String) tuple.get("priUrl"));
			http.setPubUrl((String) tuple.get("pubUrl"));
			http.setType((String) tuple.get("type"));
			http.setStatus((String) tuple.get("status"));
			http.setDataFormate((String) tuple.get("dataFormate"));
			http.setRequest((String) tuple.get("request"));
			http.setRequestMess((String) tuple.get("requestMess"));
			http.setContentType((String) tuple.get("contentType"));
			if (system != null) {
				http.setSystemCode(system.getCode());
			}
			http.setVersion((String) tuple.get("version"));
			http.setOperationType((String) tuple.get("operationType"));
			http.setSoapActionUri((String) tuple.get("soapActionUri"));
			https.put(http.getId(), http);
		}
		return https;
	}

	/**
	 * 根据系统标识获取接口标识
	 * 
	 * @param systemIds
	 *            ：系统标识
	 * @param ifName
	 *            :接口名称
	 * @param auditStatus
	 *            :审核状态
	 * @return
	 */
	public List<String> getHttpIdBySystemId(List<String> systemIds,
			String fieldId, String systemId, String ifName, String auditStatus) {
		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		CriteriaQuery<Tuple> cq = cb.createTupleQuery();

		// 构建FROM和JOIN部分
		Root<Http> rootIf = cq.from(Http.class);
		Join<Http, FieldSystem> joinSystem = rootIf.join("fieldSystem",
				JoinType.LEFT);
		Join<FieldSystem, Field> joinField = joinSystem.join("field",
				JoinType.LEFT);
		// 构建SELECT部分
		cq.multiselect(rootIf.get("id").alias("ifId"));

		// 构建WHERE部分
		List<Predicate> predicatesList = new ArrayList<>();
		if (StringUtils.isNotBlank(ifName)) {
			predicatesList.add(cb.like(rootIf.get("name"), "%" + ifName + "%"));
		}
		if (StringUtils.isNotBlank(auditStatus)) {
			predicatesList
					.add(cb.equal(rootIf.get("auditStatus"), auditStatus));
		}
		if (StringUtils.isNotBlank(fieldId)) {
			predicatesList.add(cb.equal(joinField.get("id"), fieldId));
		}
		if (StringUtils.isNotBlank(systemId)) {
			predicatesList.add(cb.equal(joinSystem.get("id"), systemId));
		}
		if (systemIds != null && systemIds.size() > 0) {
			predicatesList.add(joinSystem.get("id").in(systemIds));
		}
		if (predicatesList.size() > 0) {
			cq.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
		}

		predicatesList.add(cb.equal(rootIf.get("auditStatus"),
				AuditStatusType.AUDITED.ValueCode));

		predicatesList.add(cb.equal(rootIf.get("isEnable"), EnableType.ENABLE));
		predicatesList.add(cb.equal(rootIf.get("operationType"),
				IfOperationType.QUERY.ValueCode));
		// 执行查询
		TypedQuery<Tuple> q = entityManager.createQuery(cq);

		List<Tuple> queryResult = q.getResultList();

		if (queryResult == null || queryResult.size() < 1) {
			return null;
		}
		List<String> ifIds = new ArrayList<>(queryResult.size());

		for (Tuple tuple : queryResult) {
			ifIds.add((String) tuple.get("ifId"));
		}
		return ifIds;
	}

	/**
	 * 獲取接口參數
	 * 
	 * @param interfaceId
	 * @param string
	 * @return
	 */
	public Set<IfParameter> getInterfaceParameter(String interfaceId,
			String paraType) {

		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		cb = entityManager.getCriteriaBuilder();
		CriteriaQuery<Tuple> paramCq = cb.createTupleQuery();
		// 构建FROM和JOIN部分
		Root<IfParameter> rootParam = paramCq.from(IfParameter.class);
		paramCq.multiselect(rootParam.get("dataType").alias("dataType"),
				rootParam.get("name").alias("name"), rootParam.get("isNeed")
						.alias("isNeed"));

		// 构建WHERE部分
		List<Predicate> predicatesList = new ArrayList<>();
		predicatesList.add(cb.equal(rootParam.get("http").get("id"),
				interfaceId));
		predicatesList.add(cb.equal(rootParam.get("type"), paraType));
		paramCq.where(predicatesList.toArray(new Predicate[predicatesList
				.size()]));

		// 执行查询
		TypedQuery<Tuple> paramQ = entityManager.createQuery(paramCq);
		List<Tuple> paraQueryResult = paramQ.getResultList();

		if (paraQueryResult == null || paraQueryResult.size() == 0) {
			return null;
		}
		Set<IfParameter> set = new HashSet<>();
		for (Tuple tuple : paraQueryResult) {
			IfParameter parameter = new IfParameter();
			parameter.setDataType((String) tuple.get("dataType"));
			parameter.setName((String) tuple.get("name"));
			parameter.setIsNeed((String) tuple.get("isNeed"));
			set.add(parameter);
		}
		return set;
	}

	/**
	 * 獲取接口參數
	 * 
	 * @param interfaceId
	 * @param string
	 * @return
	 */
	public List<IfParameter> getInterfaceParameter(
			List<String> interfaceIdList, String paraType) {

		CriteriaBuilder cb = entityManager.getCriteriaBuilder();
		cb = entityManager.getCriteriaBuilder();
		CriteriaQuery<Tuple> paramCq = cb.createTupleQuery();
		// 构建FROM和JOIN部分
		Root<IfParameter> rootParam = paramCq.from(IfParameter.class);
		paramCq.multiselect(rootParam.get("http").get("id")
				.alias("interfaceId"),
				rootParam.get("dataType").alias("dataType"),
				rootParam.get("name").alias("name"), rootParam.get("isNeed")
						.alias("isNeed"));

		// 构建WHERE部分
		List<Predicate> predicatesList = new ArrayList<>();
		/*
		 * predicatesList.add(cb.in(rootParam.get("http").get("id")
		 * .in(interfaceIdList)));
		 */
		predicatesList.add(rootParam.get("http").get("id").in(interfaceIdList));
		predicatesList.add(cb.equal(rootParam.get("type"), paraType));
		paramCq.where(predicatesList.toArray(new Predicate[predicatesList
				.size()]));

		// 执行查询
		TypedQuery<Tuple> paramQ = entityManager.createQuery(paramCq);
		List<Tuple> paraQueryResult = paramQ.getResultList();

		if (paraQueryResult == null || paraQueryResult.size() == 0) {
			return null;
		}
		List<IfParameter> list = new ArrayList<>(paraQueryResult.size());
		Map<String, Http> https = new HashMap<>();
		String ifId = "";
		for (Tuple tuple : paraQueryResult) {
			ifId = (String) tuple.get("interfaceId");
			Http http = https.get(ifId);
			if (http == null) {
				http = new Http();
				http.setId(ifId);
				https.put(ifId, http);
			}

			IfParameter parameter = new IfParameter();
			parameter.setHttp(http);
			parameter.setDataType((String) tuple.get("dataType"));
			parameter.setName((String) tuple.get("name"));
			parameter.setIsNeed((String) tuple.get("isNeed"));
			list.add(parameter);
		}
		return list;
	}

	/**
	 * 申请令牌
	 * 
	 * @param applyKey
	 *            ：密钥
	 * @param systemCode
	 *            ：服务发布方编码
	 * @param interfaceCode
	 *            ：接口编码
	 * @param version
	 *            ：接口版本
	 * @return
	 * @throws IOException
	 */
	public String aquiredToken(String uri, String applyKey, String systemCode,
			String interfaceCode, String version) throws IOException {
		String url = "http://" + uri + CommonUtils.ACQUIRED_TOKEN_URI;
		url += "?" + CommonUtils.APPLY_KEY + "=" + applyKey + "&"
				+ CommonUtils.SYSTEM_CODE + "=" + systemCode + "&"
				+ CommonUtils.INTERFACE_CODE + "=" + interfaceCode + "&"
				+ CommonUtils.INTERFACE_VERSION + "=" + version;
		HttpClient httpClient = new DefaultHttpClient();

		HttpGet method = new HttpGet(url);
		method.setHeader("Content-Type", "text/html;charset=UTF-8");
		HttpResponse response;
		String result = "";
		try {
			response = httpClient.execute(method);
			result = EntityUtils.toString(response.getEntity());
		} catch (IOException e) {
			Map<String, Object> map = new HashMap<>();
			map.put("ErrMsg", "申请访问令牌失败，详细信息：" + e.getMessage());
			result = GsonUtil.getDefaultGson().toJson(map);
		} finally {

		}
		return result;
	}

	/**
	 * 根据系统标识获取接口标识
	 * 
	 * @param systemIds
	 *            ：系统标识
	 * @param ifName
	 *            :接口名称
	 * @return
	 */
	public Map<String, Boolean> getSystemExistsHttp(List<String> systemIds,
			String auditStatus, String operateType, String isEnable) {
		String sql = "";
		if (StringUtils.isEmpty(auditStatus)
				&& StringUtils.isEmpty(operateType)) {
			sql = "SELECT s.FIELD_SYSTEM_ID FROM t_cook_bu_field_system s "
					+ "WHERE (s.FIELD_SYSTEM_ID IN(:systemIds) OR EXISTS(SELECT h.IF_HTTP_ID FROM t_esb_bu_if_http h WHERE h.IS_PUBLIC='1' AND h.IS_ENABLE=:isEnable)) "
					+ " AND EXISTS(SELECT h.IF_HTTP_ID FROM t_esb_bu_if_http h WHERE h.FIELD_SYSTEM_ID=s.FIELD_SYSTEM_ID "
					+ " AND h.IS_ENABLE=:isEnable)";
		} else {
			sql = "SELECT s.FIELD_SYSTEM_ID FROM t_cook_bu_field_system s "
					+ " WHERE (s.FIELD_SYSTEM_ID IN(:systemIds) OR EXISTS(SELECT h.IF_HTTP_ID FROM t_esb_bu_if_http h WHERE h.IS_PUBLIC='1' AND h.IS_ENABLE=:isEnable))"
					+ "AND EXISTS(SELECT h.IF_HTTP_ID FROM t_esb_bu_if_http h WHERE h.FIELD_SYSTEM_ID=s.FIELD_SYSTEM_ID "
					+ "AND h.AUDIT_STATUS=:auditStatus AND h.IF_OPERATE_TYPE=:operateType AND h.IS_ENABLE=:isEnable)";
		}

		Query query = entityManager.createNativeQuery(sql);
		query.setParameter("systemIds", systemIds);

		query.setParameter("isEnable", isEnable);
		if (StringUtils.isEmpty(auditStatus)
				&& StringUtils.isEmpty(operateType)) {
		} else {
			query.setParameter("auditStatus", auditStatus);
			query.setParameter("operateType", operateType);
		}

		// 获取结果集
		List<String> objects = query.getResultList();
		if (objects == null || objects.isEmpty()) {
			return new HashMap<>();
		}
		Map<String, Boolean> map = new HashMap<>(objects.size());
		// 转换结果集
		for (String object : objects) {
			map.put(object, true);
		}
		return map;
	}

	/**
	 * 根据系统编码获取HTTP接口
	 * 
	 * @param systemCode
	 *            ：系统编码
	 * @param auditStatus
	 *            ：接口审核状态
	 * @param isEnable
	 *            ：接口是否可用
	 * @param operateType
	 *            :接口操作类型
	 * @param userId
	 *            :用户标识
	 * @return
	 */
	public List<Http> getHttpInterfaceInfos(String systemId,
			String auditStatus, String isEnable, String operateType,
			String userId) {
		String sql = "";
		if (StringUtils.isEmpty(auditStatus)) {
			sql = "SELECT h.IF_HTTP_ID,h.IF_CODE,h.IF_NAME, h.AUDIT_STATUS, h.IF_VERSION FROM t_esb_bu_if_http h WHERE h.FIELD_SYSTEM_ID=:systemId "
					+ "AND h.IF_OPERATE_TYPE=:operateType AND h.IS_ENABLE=:isEnable";
		} else {
			sql = "SELECT h.IF_HTTP_ID,h.IF_CODE,h.IF_NAME, h.AUDIT_STATUS, h.IF_VERSION FROM t_esb_bu_if_http h WHERE h.FIELD_SYSTEM_ID=:systemId "
					+ "AND h.AUDIT_STATUS=:auditStatus AND h.IF_OPERATE_TYPE=:operateType AND h.IS_ENABLE=:isEnable ";
		}
		sql += " AND (h.IS_PUBLIC='1' 	"
				+ "OR EXISTS(SELECT tu.field_system_id FROM t_cook_bu_user_system tu WHERE h.field_system_id = tu.field_system_id AND tu.USER_ID =:userId)"
				+ "OR EXISTS(SELECT * FROM t_esb_bu_if_visiblity v WHERE v.IF_ID=h.IF_HTTP_ID AND "
				+ "	EXISTS(SELECT tu.field_system_id FROM t_cook_bu_user_system tu WHERE v.IF_VISIBLE_SYSTEM_ID = tu.field_system_id AND tu.USER_ID =:userId)) )";
		Query query = entityManager.createNativeQuery(sql);
		query.setParameter("systemId", systemId);
		query.setParameter("operateType", operateType);
		query.setParameter("isEnable", isEnable);
		query.setParameter("userId", userId);
		if (StringUtils.isEmpty(auditStatus)) {

		} else {
			query.setParameter("auditStatus", auditStatus);
		}
		// 获取结果集
		List<Object[]> objects = query.getResultList();
		if (objects == null || objects.isEmpty()) {
			return new ArrayList<>(0);
		}
		List<Http> list = new ArrayList<>(objects.size());
		// 转换结果集
		for (Object[] object : objects) {
			Http http = new Http();
			http.setId((String) object[0]);
			http.setCode((String) object[1]);
			http.setName((String) object[2]);
			http.setAuditStatus((String) object[3]);
			http.setVersion((String) object[4]);
			list.add(http);
		}
		return list;
	}

}
